﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using SQLServer.DBUtility;
using System.Net;
using System.IO;

namespace Tenancy.Helper
{
    public static class GenerClass
    {
        public static string HBStartStr = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOup55rex/LeE/ZO"; //验证接口用的字段
       
        static string codeSuccess = "0";
        static string codeParamError = "-1";//"paramError"; 参数错误
        static string codeChangeError = "-2";//"changeError"; 执行sql语句错误 

        /// <summary>
        /// 返回数据
        /// </summary>
        /// <param name="msg">执行sql语句成功</param>
        public static string SetSucessJsData(string msg, string strData)
        {
            return GetJsonData(codeSuccess, msg, strData);
        }

        /// <summary>
        /// Json报错
        /// </summary>
        /// <param name="msg">参数错误信息</param>
        public static string SetParamErrorJsData(string msg)
        {
            return GetJsonData(codeParamError, msg, "[]");
        }

        /// <summary>
        /// sql语句报错
        /// </summary>
        /// <param name="msg">执行sql语句错误信息</param>
        public static string SetChangeErrorJsData(string msg)
        {
            return GetJsonData(codeChangeError, msg, "[]");
        }

        /// <summary>
        /// 返回数据
        /// </summary>
        /// <param name="msg">信息提示</param>
        /// <returns></returns>
        static string GetJsonData(string code, string msg, string strData)
        {
            StringBuilder sb = new StringBuilder("{");
            sb.Append(@"
    ");
            sb.AppendFormat("\"code\":\"{0}\",", code);
            sb.Append(@"
    ");
            sb.AppendFormat("\"msg\":\"{0}\",", msg);
            sb.Append(@"
    ");
            sb.AppendFormat("\"data\":{0}", strData);
            sb.Append(@"
}");
            return sb.ToString();
        }

        public static string GetJsonTable(string msg, string strData)
        {
            StringBuilder sb = new StringBuilder("{");
            sb.Append(@"
    ");
            sb.AppendFormat("\"code\":\"{0}\",", codeSuccess);
            sb.Append(@"
    ");
            sb.AppendFormat("\"msg\":\"{0}\",", msg);
            sb.Append(@"
    ");
            sb.AppendFormat("\"data\":{0}", strData);
            sb.Append(@"
}");
            return sb.ToString();
        }

        /// <summary>
        /// 传递一个参数的json
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="strData"></param>
        /// <returns></returns>
        public static string GetSignJson(string msg, string one)
        {
            StringBuilder sb = new StringBuilder("{");
            sb.Append(@"
    ");
            sb.AppendFormat("\"code\":\"{0}\",", codeSuccess);
            sb.Append(@"
    ");
            sb.AppendFormat("\"msg\":\"{0}\",", msg);
            sb.Append(@"
    ");
            sb.AppendFormat("\"data\":\"{0}\",", one);
            sb.Append(@"
}");
            return sb.ToString();
        }

        /// <summary>
        /// 随机生成6位数字
        /// </summary>
        public static string CreateNum()
        {
            string strTest = string.Empty;
            Random r = new Random();
            for (int i = 0; i < 6; i++)
            {
                strTest += r.Next(0, 9).ToString();
            }
            return strTest;
        }

        /// <summary>
        /// 验证码手机号是否重复
        /// </summary>
        /// <param name="Tel"></param>
        /// <returns></returns>
        public static bool TelISNORepeat(string Tel)
        {
            string TelStr = string.Format("select * from Hbung_CompanyInfo where CellTel='{0}' ", Tel);
            DataTable HB_TelDate = DBHelpSQL.Query(TelStr).Tables[0];
            if(HB_TelDate!=null&&HB_TelDate.Rows.Count>0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

       /// <summary>
       /// 判断签名是否匹配
       /// </summary>
       /// <param name="tel"></param>
       /// <returns></returns>
        public static string IsOkMatch(string tel)
        {
            string HB_Rightstr = HBStartStr + tel;
            return DESEncrypt.MD5Encrypt(HB_Rightstr);
        }

        /// <summary>
        /// 短信模板 
        /// </summary>
        /// <returns></returns>
        public static string MouldSMS(string Name,string Tel,string ModelType,string STime,string ETime,string UsePlace)
        {
            string MsgStr = "客户名:" + Name + "*电话:" + Tel + "*机具类型:" + ModelType + "*租赁时间:" + STime + "-" + ETime + "*使用地:" + UsePlace+"【利星行租赁】";
            return MsgStr;
        }

        /// <summary>
        /// 取消订单的模板
        /// </summary>
        /// <param name="StrContent"></param>
        /// <returns></returns>
        public static string CancelOrder(string StrContent)
        {
            string HXStr = "【利星行租赁】 订单号为：" + StrContent + "的订单已被用户取消";
            return HXStr;
        }

        /// <summary>
        /// 请求一个地址post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string HttpPost(string url, string param)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;

            StreamWriter requestStream = null;
            WebResponse response = null;
            string responseStr = null;

            try
            {
                requestStream = new StreamWriter(request.GetRequestStream());
                requestStream.Write(param);
                requestStream.Close();

                response = request.GetResponse();
                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                requestStream = null;
                response = null;
            }

            return responseStr;
        }

        /// <summary>
        /// get一个请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            //request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;

            WebResponse response = null;
            string responseStr = null;

            try
            {
                response = request.GetResponse();

                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                response = null;
            }

            return responseStr;
        }

        /// <summary>
        /// 从字符串里随机得到，规定个数的字符串.
        /// </summary>
        /// <param name="allChar"></param>
        /// <param name="CodeCount"></param>
        /// <returns></returns>
        public static string GetRandomCode()
        {
            string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            string[] allCharArray = allChar.Split(',');
            string RandomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < 28; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(allCharArray.Length - 1);
                while (temp == t)
                {
                    t = rand.Next(allCharArray.Length - 1);
                }
                temp = t;
                RandomCode += allCharArray[t];
            }
            return RandomCode;
        }

    }
}